package com.activitycode.adapter;

import java.util.List;

import com.activitycode.adapter.GroundListAdapter.ViewHolder;
import com.activitycode.datamodel.twitter.comment.CommentList;
import com.activitycode.tools.OptionsTool;
import com.activitycode.tools.TimeTools;
import com.activitycode.util.CommConstant;
import com.duostec.zhuyue.pets.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class CommentListAdapter extends BaseAdapter {
	private List<CommentList> comList;
	private Context context;
	private ImageLoader imageLoader = ImageLoader.getInstance();
	private DisplayImageOptions headOptions;

	public CommentListAdapter(Context context, List<CommentList> comList) {
		this.context = context;
		this.comList = comList;
		headOptions = OptionsTool.getHeadOptions(context);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return comList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return comList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup arg2) {
		ViewHolder viewHolder = null;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(
					R.layout.adapter_twittercomment_item, null);
			viewHolder.keeptername=(TextView)convertView.findViewById(R.id.keepername_textview);
			viewHolder.commentText=(TextView)convertView.findViewById(R.id.comment_textview);
			viewHolder.huifu_textView1= (TextView)convertView.findViewById(R.id.huifu_textView1);
			viewHolder.receiver_name = (TextView)convertView.findViewById(R.id.receiver_name);
			
			viewHolder.headImage = (ImageView) convertView.findViewById(R.id.personalImageId);
			viewHolder.item_name = (TextView) convertView.findViewById(R.id.item_name);
			viewHolder.item_add_time = (TextView) convertView.findViewById(R.id.item_add_time);
			convertView.setTag(viewHolder);
		} else {

			viewHolder = (ViewHolder) convertView.getTag();
		}
		imageLoader.displayImage(CommConstant.SERVICE_HOST_IP_LAN_IMG
				+ comList.get(position).getHead_image(), viewHolder.headImage,headOptions);
		viewHolder.item_name.setText(comList.get(position).getKeeper_name());
		String time1 = TimeTools.getTimeGap1(comList.get(position)
				.getAdd_time().toString());
		viewHolder.item_add_time.setText(time1);
		
		
		if("0".equals(comList.get(position).getReceive_keeper())){
			viewHolder.huifu_textView1.setVisibility(View.GONE);
			viewHolder.receiver_name.setVisibility(View.GONE);
		}else {
			viewHolder.huifu_textView1.setVisibility(View.VISIBLE);
			viewHolder.receiver_name.setVisibility(View.VISIBLE);
			viewHolder.receiver_name.setText(getReceverName(comList.get(position).getReceive_keeper()));
		}
		
		viewHolder.keeptername.setText(comList.get(position).getKeeper_name());
		
		viewHolder.commentText.setText(comList.get(position).getContent());
		if(comList.get(position).getReceiver_name()!=null){
			viewHolder.huifu_textView1.setVisibility(View.VISIBLE);
			viewHolder.receiver_name.setVisibility(View.VISIBLE);
			viewHolder.receiver_name.setText(comList.get(position).getReceiver_name());
		}
		return convertView;
	}

	public String getReceverName(String receive_keeper){
		String rNameString = "";
		for(int i = 0;i<comList.size();i++){
			if(receive_keeper.equals(comList.get(i).getKeeper_id())){
				rNameString = comList.get(i).getKeeper_name();
			}
		}
		return rNameString;
	}
	public final static class ViewHolder {
		TextView keeptername,commentText,huifu_textView1,receiver_name
					,item_name,item_add_time;
		ImageView headImage;
		
		
	}
}
